package actionThreads;

import playerData.PlayerMove;
import gui.MainFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import loadData.Resources;
import weapons.Weapon;
import weapons.WeaponSammlung;

public class PaintThread extends Thread {

    private Image screenImage, backgroundImage;
    private Graphics sIG;
    private boolean threadRunning, threadPause;
    private PlayerMove pm1, pm2;
    private WeaponSammlung wpS;
    private Resources res;

    public PaintThread(PlayerMove pm1, PlayerMove pm2, WeaponSammlung wpS, Resources res) {
        this.res = res;
        this.pm1 = pm1;
        this.pm2 = pm2;
        this.wpS = wpS;
        this.backgroundImage = res.getBackground();
        threadRunning = false;
        threadPause = false;
    }

    @Override
    public void run() {
        threadRunning = true;
        while (threadRunning) {
            if (threadPause) {
                continue;
            }
            paintScreen();
        }
    }

    private void paintScreen() {
        //Screenbild erstellen
        BufferedImage screenImageBI = new BufferedImage(MainFrame.pnSpielflaeche.getWidth(), MainFrame.pnSpielflaeche.getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        //Grafkikkontext von dem BufferedImage
        sIG = screenImageBI.getGraphics();
        //Hintergrund zeichnen
        sIG.drawImage(backgroundImage, 0, 0, MainFrame.pnSpielflaeche.getWidth(), MainFrame.pnSpielflaeche.getHeight(), null);
        if (!pm1.getDead()) {
            //Player 1 zeichnen
            sIG.drawImage(pm1.getActivePic(), pm1.getX(), pm1.getY(), pm1.getImageWidth(), pm1.getImageHeight(), null);
        }
        if (!pm2.getDead()) {
            //Player 2 zeichnen
            sIG.drawImage(pm2.getActivePic(), pm2.getX(), pm2.getY(), pm2.getImageWidth(), pm2.getImageHeight(), null);
        }
        //Alle Weapons zeichnen
        for (int i = wpS.size() - 1; i >= 0; i--) {
            Weapon wp = wpS.get(i);
            if (!wp.getCollide()) {
                sIG.drawImage(wp.getWeaponImg(), wp.getX(), wp.getY(), wp.getImageWidth(),
                        wp.getImageHeight(), null);
            } else {
                //Nur als ungefährer Code NOT WORKING!!
                /*if (wp.getExplosion()) {
                sIG.drawImage(res.getExplosion(), wp.getX(), wp.getY(),null);
                } else {
                wpS.remove(wp);
                }*/
                wpS.remove(wp);
            }
        }
        //Namen zeichnen für P1 und P2
        //Farbe auf schwarz setzen
        sIG.setColor(Color.BLACK);
        //Pixel-Länge der beiden Namen in Variablen speichern
        int length1 = sIG.getFontMetrics().stringWidth(pm1.getPlayersName()) / 2;
        int length2 = sIG.getFontMetrics().stringWidth(pm2.getPlayersName()) / 2;
        //Namen zeichnen
        if (!pm1.getDead()) {
            sIG.drawString(pm1.getPlayersName(), pm1.getX() + (pm1.getImageWidth() / 2) - length1, pm1.getY() - 5);
        }
        if (!pm2.getDead()) {
            sIG.drawString(pm2.getPlayersName(), pm2.getX() + (pm2.getImageWidth() / 2) - length2, pm2.getY() - 5);
        }
        //Screenbild in screenImage speichern
        screenImage = screenImageBI;
        //Screenbild auf die Spielfläche zeichnen
        MainFrame.pnSpielflaeche.getGraphics().drawImage(screenImage, 0, 0, MainFrame.pnSpielflaeche.getWidth(), MainFrame.pnSpielflaeche.getHeight(), null);
    }

    public void pauseThread() {
        threadPause = true;
    }

    public void resumeThread() {
        threadPause = false;
    }

    public void stopThread() {
        threadRunning = false;
    }

    public Image getScreen() {
        return screenImage;
    }
}
